package com.zsqat.eshop.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.zsqat.eshop.ware.vo.MergeVo;
import com.zsqat.eshop.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zsqat.eshop.ware.entity.PurchaseEntity;
import com.zsqat.eshop.ware.service.PurchaseService;
import com.zsqat.common.utils.PageUtils;
import com.zsqat.common.utils.R;


/**
 * 采购信息
 *
 * @author zsqat
 * @email 491192853@qq.com
 * @date 2020-11-19 15:37:52
 */
@RestController
@RequestMapping("ware/purchase" )
public class PurchaseController {
  @Autowired
  private PurchaseService purchaseService;

  // 未领取采购订单，以便合并订单时查找
  ///ware/purchase/unreceive/list
  @GetMapping(value = "/unreceive/list")
  public R unreceiveList(@RequestParam Map<String, Object> params){
    PageUtils page = purchaseService.queryPageUnreceive(params);

    return R.ok().put("page", page);
  }


  /**
   * 列表
   */
  @RequestMapping("/list" )
  // @RequiresPermissions("ware:purchase:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = purchaseService.queryPage(params);
    return R.ok().put("page", page);
  }

  // 合并订单
  @PostMapping(value = "/merge")
  public R merge(@RequestBody MergeVo mergeVo) {

    purchaseService.mergePurchase(mergeVo);

    return R.ok();
  }

  // 领取采购单(采购人员通过手机领取属于自己名下的采购单)
  @PostMapping(value = "/received")
  public R received(@RequestBody List<Long> ids) {
    purchaseService.received(ids);
    return R.ok();
  }

  // 完成采购单(采购人员通过手机完成属于自己名下的采购单，没完成的需要写没完成原因)
  @PostMapping(value = "/done")
  public R finish(@RequestBody PurchaseDoneVo doneVo) {
    purchaseService.done(doneVo);
    return R.ok();
  }

  @RequestMapping("/info/{id}" )
  // @RequiresPermissions("ware:purchase:info")
  public R info(@PathVariable("id" ) Long id) {
    PurchaseEntity purchase = purchaseService.getById(id);

    return R.ok().put("purchase", purchase);
  }

  /**
   * 保存
   */
  @RequestMapping("/save" )
  // @RequiresPermissions("ware:purchase:save")
  public R save(@RequestBody PurchaseEntity purchase) {
    purchase.setCreateTime(new Date());
    purchase.setUpdateTime(new Date());
    purchaseService.save(purchase);
    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update" )
  // @RequiresPermissions("ware:purchase:update")
  public R update(@RequestBody PurchaseEntity purchase) {
    purchaseService.updateById(purchase);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete" )
  // @RequiresPermissions("ware:purchase:delete")
  public R delete(@RequestBody Long[] ids) {
    purchaseService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }

}
